package com.ticket.web.controller.yanchu;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.utils.SecurityUtils;
import com.ticket.system.domain.YanchuScore;
import com.ticket.system.domain.vo.YanchuScoreDetailVO;
import com.ticket.system.service.IYanchuScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.enums.BusinessType;
import com.ticket.system.domain.YanchuScoreDetail;
import com.ticket.system.service.IYanchuScoreDetailService;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.common.core.page.TableDataInfo;

/**
 * 积分明细Controller
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/system/score/detail")
@Api(tags = "积分明细")
public class YanchuScoreDetailController extends BaseController
{
    @Autowired
    private IYanchuScoreDetailService yanchuScoreDetailService;
    @Autowired
    private IYanchuScoreService iYanchuScoreService;

    /**
     * 查询【积分明细】列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:list')")
    @GetMapping("/list")
    @ApiOperation("管控台积分明细列表")
    public TableDataInfo list(YanchuScoreDetail yanchuScoreDetail)
    {
        startPage();
        List<YanchuScoreDetail> list = yanchuScoreDetailService.selectYanchuScoreDetailList(yanchuScoreDetail);
        return getDataTable(list);
    }

    @GetMapping("/getUserScoreList")
    @ApiOperation("小程序用户获得积分明细列表")
    public AjaxResult getUserScoreList()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        YanchuScoreDetail yanchuScoreDetail = new YanchuScoreDetail();
        yanchuScoreDetail.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<YanchuScoreDetail> list = yanchuScoreDetailService.selectYanchuScoreDetailList(yanchuScoreDetail);
        YanchuScoreDetailVO yanchuScoreDetailVO = new YanchuScoreDetailVO();
        yanchuScoreDetailVO.setYanchuScoreDetailList(getDataTable(list));
        YanchuScore yanchuScore = new YanchuScore();
        yanchuScore.setWxUserId(loginUser.getUser().getUserId());
        List<YanchuScore> yanchuScores = iYanchuScoreService.selectYanchuScoreList(yanchuScore);
        if(CollectionUtils.isNotEmpty(yanchuScores)){
            yanchuScoreDetailVO.setScore(yanchuScores.get(0).getScore());
        }else {
            yanchuScoreDetailVO.setScore(0);
        }
        return AjaxResult.success(yanchuScoreDetailVO);
    }

    /**
     * 导出【积分明细】列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:export')")
    @Log(title = "【导出积分明细】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("管控台导出积分明细")
    public void export(HttpServletResponse response, YanchuScoreDetail yanchuScoreDetail)
    {
        List<YanchuScoreDetail> list = yanchuScoreDetailService.selectYanchuScoreDetailList(yanchuScoreDetail);
        ExcelUtil<YanchuScoreDetail> util = new ExcelUtil<YanchuScoreDetail>(YanchuScoreDetail.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【积分明细】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:detail:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获得积分明细详情")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuScoreDetailService.selectYanchuScoreDetailById(id));
    }

    /**
     * 新增【积分明细】
     */
    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "【新增积分明细】", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增积分明细")
    public AjaxResult add(@RequestBody YanchuScoreDetail yanchuScoreDetail)
    {
        return toAjax(yanchuScoreDetailService.insertYanchuScoreDetail(yanchuScoreDetail));
    }

    /**
     * 修改【积分明细】
     */
    @PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @Log(title = "修改【积分明细】", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改【积分明细】")
    public AjaxResult edit(@RequestBody YanchuScoreDetail yanchuScoreDetail)
    {
        return toAjax(yanchuScoreDetailService.updateYanchuScoreDetail(yanchuScoreDetail));
    }

    /**
     * 删除【积分明细】
     */
    @PreAuthorize("@ss.hasPermi('system:detail:remove')")
    @Log(title = "【删除积分明细】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除积分明细")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuScoreDetailService.deleteYanchuScoreDetailByIds(ids));
    }
}
